package com.ruoyix.novel.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyix.common.utils.StringUtils;
import com.ruoyix.common.core.page.TableDataInfo;
import com.ruoyix.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyix.novel.domain.bo.NovelCharacterBo;
import com.ruoyix.novel.domain.vo.NovelCharacterVo;
import com.ruoyix.novel.domain.NovelCharacter;
import com.ruoyix.novel.mapper.NovelCharacterMapper;
import com.ruoyix.novel.service.INovelCharacterService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 角色设定Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-05
 */
@RequiredArgsConstructor
@Service
public class NovelCharacterServiceImpl implements INovelCharacterService {

    private final NovelCharacterMapper baseMapper;

    /**
     * 查询角色设定
     */
    @Override
    public NovelCharacterVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询角色设定列表
     */
    @Override
    public TableDataInfo<NovelCharacterVo> queryPageList(NovelCharacterBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NovelCharacter> lqw = buildQueryWrapper(bo);
        Page<NovelCharacterVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询角色设定列表
     */
    @Override
    public List<NovelCharacterVo> queryList(NovelCharacterBo bo) {
        LambdaQueryWrapper<NovelCharacter> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NovelCharacter> buildQueryWrapper(NovelCharacterBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NovelCharacter> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getProjectId() != null, NovelCharacter::getProjectId, bo.getProjectId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), NovelCharacter::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getGender()), NovelCharacter::getGender, bo.getGender());
        lqw.eq(bo.getAge() != null, NovelCharacter::getAge, bo.getAge());
        lqw.eq(StringUtils.isNotBlank(bo.getRoleType()), NovelCharacter::getRoleType, bo.getRoleType());
        lqw.eq(StringUtils.isNotBlank(bo.getPersonality()), NovelCharacter::getPersonality, bo.getPersonality());
        lqw.eq(StringUtils.isNotBlank(bo.getBackground()), NovelCharacter::getBackground, bo.getBackground());
        lqw.eq(StringUtils.isNotBlank(bo.getImageUrl()), NovelCharacter::getImageUrl, bo.getImageUrl());
        return lqw;
    }

    /**
     * 新增角色设定
     */
    @Override
    public Boolean insertByBo(NovelCharacterBo bo) {
        NovelCharacter add = BeanUtil.toBean(bo, NovelCharacter.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改角色设定
     */
    @Override
    public Boolean updateByBo(NovelCharacterBo bo) {
        NovelCharacter update = BeanUtil.toBean(bo, NovelCharacter.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NovelCharacter entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除角色设定
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
